//
//  QRCodeMyCardViewController.swift
//  weibo_swift
//
//  Created by dulinlong on 15/7/2.
//  Copyright © 2015年 sheji_56. All rights reserved.
//

import UIKit

class QRCodeMyCardViewController: UIViewController {

    lazy var iconImageView = UIImageView()
    
    override func loadView() {
        view = UIView()
        
        view.backgroundColor = UIColor.whiteColor()
        view.addSubview(iconImageView)
        setupConstraint()
        iconImageView.backgroundColor = UIColor.lightGrayColor()

    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
     iconImageView.image = generateQRCodeImage()
        
    }

   private func setupConstraint(){
        iconImageView.translatesAutoresizingMaskIntoConstraints = false
        view.addConstraint(NSLayoutConstraint(item: iconImageView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: iconImageView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        iconImageView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[view(200)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["view":iconImageView]))
        iconImageView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[view(200)]", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: ["view":iconImageView]))
    }
    
    /// 生成二维码
    private func generateQRCodeImage() -> UIImage {
        
        // 1. 生成二维码
        let qrFilter = CIFilter(name: "CIQRCodeGenerator")!
         // 2. 设置成初始值
        qrFilter.setDefaults()
        // 3. 通过 KVC 设置滤镜信息  ,二维码所包含的信息,一般为网址
        qrFilter.setValue("sheji_56".dataUsingEncoding(NSUTF8StringEncoding), forKey: "inputMessage")
        // 4. 获取结果
        let ciImage = qrFilter.outputImage
        
        // 2. 缩放处理
        let transform = CGAffineTransformMakeScale(10, 10)
        let transformImage = ciImage.imageByApplyingTransform(transform)
        
        // 3. 颜色滤镜
        let colorFilter = CIFilter(name: "CIFalseColor")!
        colorFilter.setDefaults()
        colorFilter.setValue(transformImage, forKey: "inputImage")
        // 前景色
        colorFilter.setValue(CIColor(color: UIColor.blackColor()), forKey: "inputColor0")
        // 背景色
        colorFilter.setValue(CIColor(color: UIColor.whiteColor()), forKey: "inputColor1")
        
        let outputImage = colorFilter.outputImage
        

        return insertAvatarImage(UIImage(CIImage: outputImage), avatar: UIImage(named: "avatar")!)
    }
    
    //插入头像
    func insertAvatarImage(qrimage: UIImage, avatar: UIImage) -> UIImage {
        
        UIGraphicsBeginImageContext(qrimage.size)
        
        let rect = CGRect(origin: CGPointZero, size: qrimage.size)
        qrimage.drawInRect(rect)
        
        let w = rect.width * 0.2
        let x = (rect.width - w) * 0.5
        let y = (rect.height - w) * 0.5
        avatar.drawInRect(CGRect(x: x, y: y, width: w, height: w))
        
        let image = UIGraphicsGetImageFromCurrentImageContext()
        
        UIGraphicsEndImageContext()
        
        return image
    }
   
}
